﻿using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Xml;

namespace 物流管理系统
{
    internal class DBOperation
    {
        MySqlConnection conn;
        public DBOperation()
        {
            conn = new MySqlConnection("server=42.192.69.243; port=3306;user=root;password=yfy20010910;database=lms;Charset=utf8;");
        }
        public void nonquerysql(string mysqlstring)
        {
            try
            {
                conn.Open();
                MySqlCommand mscmd = new MySqlCommand(mysqlstring, conn);
                mscmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                conn.Close();
            }
        }
        public DataSet querysql(string mysqlstring, string tablename)
        {
            try
            {
                conn.Open();
                MySqlCommand mscmd = new MySqlCommand(mysqlstring, conn);
                MySqlDataAdapter msda = new MySqlDataAdapter(mscmd);
                DataSet ds = new DataSet();
                msda.Fill(ds, tablename);
                return ds;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return null;
            }
            finally
            {
                conn.Close();
            }
        }
        public int login(string uname, string pwd)
        {
            try
            {
                conn.Open();
                MySqlCommand mscmd = new MySqlCommand("select 用户编号,用户密码 from 用户信息表 where 用户编号='"+uname+"'", conn);
                MySqlDataReader msreader = mscmd.ExecuteReader();
                while (msreader.Read())
                {
                    if (uname.Equals(msreader.GetString(0)))
                    {
                        if (pwd.Equals(msreader.GetString(1)))
                        {
                            return 0;
                        }
                        else
                        {
                            return 1;
                        }
                    }
                }
                return 2;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return 3;
            }
            finally
            {
                conn.Close();
            }
        }
        public double numberreturn(string mysqlstring)
        {
            try
            {
                conn.Open();
                MySqlCommand mscmd = new MySqlCommand(mysqlstring, conn);
                MySqlDataReader msreader = mscmd.ExecuteReader();
                while (msreader.Read())
                {
                    return msreader.GetDouble(0);
                }
                return 0;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return 0;
            }
            finally
            {
                conn.Close();
            }
        }
        public bool exists(string mysqlstring, string idstr)
        {
            try
            {
                conn.Open();
                MySqlCommand mscmd = new MySqlCommand(mysqlstring, conn);
                MySqlDataReader msreader = mscmd.ExecuteReader();
                while (msreader.Read())
                {
                    if (idstr.Equals(msreader.GetString(0)))
                    {
                        return true;
                    }
                }
                return false;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return false;
            }
            finally
            {
                conn.Close();
            }
        }
        public List<string> querylist(string mysqlstring)
        {
            try
            {
                conn.Open();
                MySqlCommand mscmd = new MySqlCommand(mysqlstring, conn);
                MySqlDataReader mreader = mscmd.ExecuteReader();
                List<string> list = new List<string>();
                while (mreader.Read())
                {
                    list.Add(mreader.GetString(0));
                }
                return list;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return null;
            }
            finally
            {
                conn.Close();
            }
        }
        public List<string> queryalllist(string mysqlstring, int len)
        {
            try
            {
                conn.Open();
                MySqlCommand mscmd = new MySqlCommand(mysqlstring, conn);
                MySqlDataReader mreader = mscmd.ExecuteReader();
                List<string> list = new List<string>();
                while (mreader.Read())
                {
                    for (int i = 0; i < len; i++)
                    {
                        list.Add(mreader.GetString(i));
                    }
                }
                return list;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return null;
            }
            finally
            {
                conn.Close();
            }
        }
    }
}
